package csvs

import (
	"fmt"
	"math/rand"
)

var (
	ConfigDropItemGroupMap    map[int]*DropItemGroup
	ConfigWeaponLevelMap      map[int]map[int]*ConfigWeaponLevel
	ConfigWeaponStarMap       map[int]map[int]*ConfigWeaponStar
	ConfigNpcDialogMap		  map[int]*NpcDialogGroup
	ConfigSkillLevelMap		  map[int]map[int]*Skill
	ConfigSkillDescribeMap	  map[int]*SkillDescribeGroup
	ConfigNpcSkillMap		  map[int]*NpcSkillGroup
)

type DropItemGroup struct {
	DropId      int
	DropConfigs []*ConfigDropItem
}

type NpcDialogGroup struct {
	NpcId       int
	NpcDialogConfigs []*ConfigNpcDialog
}

type SkillDescribeGroup struct {
	SkillID       int
	SkillDescribeConfigs []*SkillDescribe
}

type NpcSkillGroup struct {
	NpcId       int
	NpcSkillConfigs	[]*ConfigNpcSkill
}

func CheckLoadCsv() {
	//二次处理

	MakeDropItemGroupMap()
	MakeConfigWeaponLevelMap()
	MakeConfigWeaponStarMap()
	MakeConfigNpcDialogMap()
	MakeConfigSkillLevelMap()
	MakeConfigSkillDescribeMap()
	MakeConfigNpcSkillMap()

	Fill_AllTGrowthLineJobAttr()

	fmt.Println("csv配置读取完成---ok")
}

func MakeDropItemGroupMap() {
	ConfigDropItemGroupMap = make(map[int]*DropItemGroup)
	for _, v := range ConfigDropItemSlice {
		dropGroup, ok := ConfigDropItemGroupMap[v.DropId]
		if !ok {
			dropGroup = new(DropItemGroup)
			dropGroup.DropId = v.DropId
			ConfigDropItemGroupMap[v.DropId] = dropGroup
		}
		dropGroup.DropConfigs = append(dropGroup.DropConfigs, v)
	}
	return
}

func MakeConfigWeaponLevelMap() {
	ConfigWeaponLevelMap = make(map[int]map[int]*ConfigWeaponLevel)
	for _, v := range ConfigWeaponLevelSlice {
		levelMap, ok := ConfigWeaponLevelMap[v.WeaponStar]
		if !ok {
			levelMap = make(map[int]*ConfigWeaponLevel)
			ConfigWeaponLevelMap[v.WeaponStar] = levelMap
		}
		levelMap[v.Level] = v
	}
	return
}

func MakeConfigWeaponStarMap() {
	ConfigWeaponStarMap = make(map[int]map[int]*ConfigWeaponStar)
	for _, v := range ConfigWeaponStarSlice {
		starMap, ok := ConfigWeaponStarMap[v.WeaponStar]
		if !ok {
			starMap = make(map[int]*ConfigWeaponStar)
			ConfigWeaponStarMap[v.WeaponStar] = starMap
		}
		starMap[v.StarLevel] = v
	}
	return
}

func MakeConfigNpcDialogMap() {
	ConfigNpcDialogMap = make(map[int]*NpcDialogGroup)
	for _, v := range ConfigNpcDialogSlice {
		dialogGroup, ok := ConfigNpcDialogMap[v.NpcId]
		if !ok {
			dialogGroup = new(NpcDialogGroup)
			dialogGroup.NpcId = v.NpcId
			ConfigNpcDialogMap[v.NpcId] = dialogGroup
		}
		dialogGroup.NpcDialogConfigs = append(dialogGroup.NpcDialogConfigs, v)
	}
	return
}

func MakeConfigSkillLevelMap() {
	ConfigSkillLevelMap = make(map[int]map[int]*Skill)
	for _, v := range ConfigSkillSlice {
		LevelMap, ok := ConfigSkillLevelMap[v.SkillID]
		if !ok {
			LevelMap = make(map[int]*Skill)
			ConfigSkillLevelMap[v.SkillID] = LevelMap
		}
		LevelMap[v.SkillLv] = v
	}
	return
}

func MakeConfigSkillDescribeMap() {
	ConfigSkillDescribeMap = make(map[int]*SkillDescribeGroup)
	for _, v := range ConfigSkillDescribeSlice {
		DescribeGroup, ok := ConfigSkillDescribeMap[v.SkillID]
		if !ok {
			DescribeGroup = new(SkillDescribeGroup)
			DescribeGroup.SkillID = v.SkillID
			ConfigSkillDescribeMap[v.SkillID] = DescribeGroup
		}
		DescribeGroup.SkillDescribeConfigs = append(DescribeGroup.SkillDescribeConfigs, v)
	}
	return
}

func MakeConfigNpcSkillMap() {
	ConfigNpcSkillMap = make(map[int]*NpcSkillGroup)
	for _, v := range ConfigNpcSkillSlice {
		SkillGroup, ok := ConfigNpcSkillMap[v.NpcId]
		if !ok {
			SkillGroup = new(NpcSkillGroup)
			SkillGroup.NpcId = v.NpcId
			ConfigNpcSkillMap[v.NpcId] = SkillGroup
		}
		SkillGroup.NpcSkillConfigs = append(SkillGroup.NpcSkillConfigs, v)
	}
	return
}

func Fill_AllTGrowthLineJobAttr() {
	for _, v := range ConfigWeaponMap {
		GetManageJobAttributeIndex().Fill_TGrowthLineJobAttr(&v.JobAttr, v.Ability)
	}
	for _, v := range ConfigPlayerLevelSlice {
		GetManageJobAttributeIndex().Fill_TGrowthLineJobAttr(&v.JobAttr, v.Ability)
	}
	for _, v := range ConfigNpcSlice {
		GetManageJobAttributeIndex().Fill_TGrowthLineJobAttr(&v.JobAttr, v.Ability)
	}
}

func GetDropItemGroup(dropId int) *DropItemGroup {
	return ConfigDropItemGroupMap[dropId]
}

func GetDropItemGroupNew(dropId int) []*ConfigDropItem {
	rel := make([]*ConfigDropItem, 0)
	if dropId == 0 {
		return rel
	}
	config := GetDropItemGroup(dropId)
	configsAll := make([]*ConfigDropItem, 0)
	for _, v := range config.DropConfigs {
		if v.DropType == DROP_ITEM_TYPE_ITEM {
			randNum := rand.Intn(PERCENT_ALL)
			if randNum < v.Weight {
				rel = append(rel, v)
			}
		} else if v.DropType == DROP_ITEM_TYPE_GROUP {
			randNum := rand.Intn(PERCENT_ALL)
			if randNum < v.Weight {
				configs := GetDropItemGroupNew(v.ItemId)
				rel = append(rel, configs...)
			}
		} else if v.DropType == DROP_ITEM_TYPE_WEIGHT {
			configsAll = append(configsAll, v)
		}
	}
	if len(configsAll) > 0 {
		allRate := 0
		for _, v := range configsAll {
			allRate += v.Weight
		}
		randNum := rand.Intn(allRate)
		nowRate := 0
		for _, v := range configsAll {
			nowRate += v.Weight
			if nowRate > randNum {
				newConfig := new(ConfigDropItem)
				newConfig.Weight = PERCENT_ALL
				newConfig.DropId = v.DropId
				newConfig.DropType = v.DropType
				newConfig.ItemId = v.ItemId
				newConfig.ItemNumMin = v.ItemNumMin
				newConfig.ItemNumMax = v.ItemNumMax
				rel = append(rel, newConfig)
				break
			}
		}
	}
	return rel
}

func GetWeaponLevelConfig(weaponStar int, level int) *ConfigWeaponLevel {
	_, ok := ConfigWeaponLevelMap[weaponStar]
	if !ok {
		return nil
	}

	_, ok = ConfigWeaponLevelMap[weaponStar][level]
	if !ok {
		return nil
	}
	return ConfigWeaponLevelMap[weaponStar][level]
}

func GetWeaponStarConfig(weaponStar int, starLevel int) *ConfigWeaponStar {
	_, ok := ConfigWeaponStarMap[weaponStar]
	if !ok {
		return nil
	}

	_, ok = ConfigWeaponStarMap[weaponStar][starLevel]
	if !ok {
		return nil
	}
	return ConfigWeaponStarMap[weaponStar][starLevel]
}

func GetNpcDialogGroup(NpcId int) *NpcDialogGroup {
	return ConfigNpcDialogMap[NpcId]
}

func GetNpcChat(NpcId int) string {
	config := GetNpcDialogGroup(NpcId)
	allRate := 0
	for _, v := range config.NpcDialogConfigs {
		allRate += v.Weight
	}
	randNum := rand.Intn(allRate)
	for _, v := range config.NpcDialogConfigs {
		randNum = randNum - v.Weight
		if randNum <= 0 {
			return v.Dialog
		}
	}

	return ""
}

func GetSkillDescribeGroup(SkillId int) *SkillDescribeGroup {
	return ConfigSkillDescribeMap[SkillId]
}

func GetSkillDescribe(SkillId int) (SkillDescribe, HitDescribe, FeelDescribe string) {
	config := GetSkillDescribeGroup(SkillId)
	allRate := 0
	for _, v := range config.SkillDescribeConfigs {
		allRate += v.Weight
	}
	randNum := rand.Intn(allRate)
	for _, v := range config.SkillDescribeConfigs {
		randNum = randNum - v.Weight
		if randNum <= 0 {
			return v.SkillDescribe, v.HitDescribe, v.FeelDescribe
		}
	}

	return "", "", ""
}

func GetNpcSkillGroup(NpcId int) *NpcSkillGroup {
	return ConfigNpcSkillMap[NpcId]
}

func GetNpcSkillId(NpcId int) (SkillID, SkillLv int) {
	config := GetNpcSkillGroup(NpcId)
	allRate := 0
	for _, v := range config.NpcSkillConfigs {
		allRate += v.Weight
	}
	randNum := rand.Intn(allRate)
	for _, v := range config.NpcSkillConfigs {
		randNum = randNum - v.Weight
		if randNum <= 0 {
			return v.SkillID, v.SkillLv
		}
	}

	return 0, 0
}